<?php 
/**
 * @name ActiveQ —— a PHP ORM based on PDO 
 * @author crazymus < QQ:291445576 >
 * @version 1.0.0
 * @licensed apache2.0
 * @updated 2015-09-14
 */
class ActiveQ{
	//static 
	protected static $_DB_CON;
	//db connect
	protected $_db_con;
	//table name
	protected $_table;
	//fields to select
	protected $_fields = '*';
	//order by
	protected $_order_by;
	//pagination
	protected $_limit;
	//where 
	protected $_condition = " 1 ";
	//param by prepare
	protected $_params = array();
	
	//record data,init by create action
	protected $_data;
	
	//database config
	protected $_config = array(
		'dsn' =>  'mysql:dbname=test;host=localhost',
		'user' => 'root',
		'pass' =>  123
	);

	public function __construct($model_name){

		try{
			if(self::$_DB_CON){
				$this->_db_con = self::$_DB_CON;
			}else{
				extract($this->_config);
				$this->_db_con = new PDO($dsn,$user,$pass);
				self::$_DB_CON = $this->_db_con;
			}
			$this->_table  = $model_name;
		}catch(PDOException $e){
			echo 'connect error:'.$e->getMessage();
		}	
	}
	
	//get a record by id or by condition
	public function find($id=null){
		if($id){
			$condition = " id = ? ";
			$params = array($id);	
		}else{
			$condition = $this->_condition;
			$params = $this->_params;
		}
		$sql = "SELECT $this->_fields FROM $this->_table
		where $condition LIMIT 1 ";
		$pdo_object = $this->_db_con->prepare($sql);
		$pdo_object->execute($params);
		$data = $pdo_object->fetch(PDO::FETCH_ASSOC);
		return $data;
	}
	
	//count the num
	public function count(){
		$sql = "SELECT count(id) AS num FROM $this->_table
		WHERE $this->_condition ";
		$pdo_object = $this->_db_con->prepare($sql);
		$pdo_object->execute($this->_params);
		$data = $pdo_object->fetch(PDO::FETCH_ASSOC);
		return $data['num'];
	}
	
	//get some records
	public function select(){
		$sql = "SELECT $this->_fields FROM $this->_table 
		where $this->_condition $this->_order_by $this->_limit ";
		$pdo_object = $this->_db_con->prepare($sql);
		$pdo_object->execute($this->_params);
		$dataset = $pdo_object->fetchAll(PDO::FETCH_ASSOC);
		return $dataset;
	}
	
	//get value of a field
	public function getField($field){
		$data = $this->field($field)->find();
		return $data?$data[$field]:null;
	}
	
	//add data
	public function add($data=null){
		$data = $data?$data:$this->_data;
		if(!is_array($data)){
			exit('no data to insert');
		}
		$keys = array();
		$values = array();
		$params = array();
		foreach($data as $key=>$value){
			$keys[] = $key;
			$values[] = '?';
			$params[] = $value;
		}
		$sql = "INSERT INTO $this->_table (".implode(',',$keys).") VALUES 
		(".implode(',',$values).")";
		$pdo_object = $this->_db_con->prepare($sql);
		$res = $pdo_object->execute($params);
		return $res;
	}
	
	//edit data
	public function save($data=null){
		$data = $data?$data:$this->data;
		if(!is_array($data)){
			exit('no data to save');
		}
		
		$key_vals = array();
		$params = array();
		foreach($data as $key=>$value){
			$key_vals[] = " $key = ? ";
			$params[] = $value;
		}

		$sql = "UPDATE $this->_table SET ".implode(',',$key_vals)." WHERE $this->_condition ";
		
		$params = array_merge($params,$this->_params);
		
		$pdo_object = $this->_db_con->prepare($sql);
		$pdo_object->execute($params);
		
		$res = $pdo_object->rowCount();
		return $res;
	}

	//delete a record or some records
	public function delete($ids=null){
		if($ids){
			$ids = $this->_intIds($ids);
			$condition = " id IN ( $ids )";
			$params = array();
		}else{
			$condition = $this->_condition;
			$params = $this->_params;
		}
		$sql = "DELETE FROM $this->_table WHERE $condition ";
		$pdo_object = $this->_db_con->prepare($sql);
		$pdo_object->execute($params);
		$count = $pdo_object->rowCount();
		return $count;
	}
	
	//trim ids 
	private function _intIds($ids){
		$arr = explode(',',$ids);
		$new_arr = array();
		foreach($arr as $val){
			$new_arr[] = intval($val);
		}
		return implode(',',$new_arr);
	}
	
	//pagination
	public function page($page,$limit){
		$start = ($page-1)*$limit;
		$this->_limit = " LIMIT $start,$limit ";
		return $this;
	}
	
	//fields to select
	public function field($fields){
		$this->_fields = $fields;
		return $this;
	}
	
	//order by 
	public function order($order_by){
		$this->_order_by = ' ORDER BY '.$order_by;
		return $this;
	}
	
	//condition,all params use prepare 
	public function where($condition,$params=array()){
		$this->_condition = $condition;
		$this->_params = $params;
		return $this;
	}
	
	//start transaction
	public function startTrans(){
		$this->_db_con->beginTransaction();
	}
	
	//commit transaction
	public function commit(){
		$this->_db_con->commit();
	}
	
	//roll back transaction
	public function rollBack(){
		$this->_db_con->rollBack();
	}

}

?>